package com.cris.Controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cris.Common.BaseContext;
import com.cris.Dto.OrdersDto;
import com.cris.Service.AddressBookService;
import com.cris.Service.OrderDetailService;
import com.cris.Service.OrderService;
import com.cris.Service.UserService;
import com.cris.entity.AddressBook;
import com.cris.entity.OrderDetail;
import com.cris.entity.Orders;
import com.cris.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){

        log.info("下单信息：",orders);

        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page> page(int page , int pageSize){
        Page<OrderDetail> pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper();
        lqw.orderByAsc(OrderDetail::getAmount);
        orderDetailService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }
//    @GetMapping("/userPage")
//    public R<Page> page(int page , int pageSize){
//        Page<OrderDetail> pageInfo = new Page(page,pageSize);
//        Page<OrdersDto> pageDto = new Page<>();
//        Long currentId = BaseContext.getCurrentId();
//        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper();
//        lqw.orderByAsc(OrderDetail::getAmount);
//        orderDetailService.page(pageInfo,lqw);
//        BeanUtils.copyProperties(pageInfo,pageDto,"records");
//        List<OrderDetail> records = pageInfo.getRecords();
//        List<OrdersDto> list = records.stream().map((item) ->{
//            OrdersDto ordersDto = new OrdersDto();
//            BeanUtils.copyProperties(item,ordersDto);
//            User user = userService.getById(currentId);
//            ordersDto.setUserName(user.getName());
//            LambdaQueryWrapper<Orders> lqw2 = new LambdaQueryWrapper<>();
//            lqw2.eq(Orders::getUserId,currentId);
//            Orders order = orderService.getById(lqw2);
//            Long addressBookId = order.getAddressBookId();
//            AddressBook addressBook = addressBookService.getById(addressBookId);
//            ordersDto.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName()) +
//                    (addressBook.getCityName() == null ? "" : addressBook.getCityName()) +
//                    (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())+
//                    (addressBook.getDetail() == null ? "" : addressBook.getDetail()));
//            ordersDto.setPhone(addressBook.getPhone());
//            ordersDto.setConsignee(addressBook.getConsignee());
//            return ordersDto;
//        }).collect(Collectors.toList());
//        pageDto.setRecords(list);
//        return R.success(pageDto);
//    }
}
